#coding: utf-8
from django.db import models
from datetime import datetime
from django.contrib.auth.models import User
# Create your models here.
class Vote(models.Model):
    user = models.ForeignKey(User, parent_link = True)
    track_id = models.IntegerField(u'Номер трека')
    vote = models.BooleanField(u'Голос', choices=((1,'rocks'),(0,'sucks')))
    datetime = models.DateTimeField(u'Дата', default=datetime.now() )
    hour_id = models.IntegerField(default=0)
    class Meta:
        unique_together = ('user', 'track_id', 'hour_id')
        ordering = ['-datetime']
    def save(self):
        if self.set_hour_id(): 
            try:
                super(Vote, self).save()
                return True
            except:
                return False
            
        else: return False
    def set_hour_id(self):
        n = datetime.now()
        s = '{0}{1}{2}{3}'.format(n.year,n.month,n.day,n.hour)
        self.hour_id = int(s)
        return True
